/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PnlActualBudget.java
 *
 * Created on May 4, 2010, 11:14:12 AM
 */
package com.aptech.campaign.ui;

import com.aptech.campaign.biz.CampaignBiz;
import com.aptech.campaign.entities.ActualBudget;
import com.aptech.campaign.entities.Campaign;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Tai Nguyen
 */
public class ActualBudgetPnl extends javax.swing.JPanel {

    CampaignBiz camBuz = new CampaignBiz();
    ArrayList<Campaign> arrCam = null;

    public ActualBudgetPnl() {
        initComponents();
        putDataForCbb();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    /**
     *
     * @return boolean value
     */
    public boolean valid() {
        if (!txfActualBudget.getText().equalsIgnoreCase("") && !txfActualSale.getText().equalsIgnoreCase("") && !txfSaleVariance.getText().equalsIgnoreCase("")
                && !txfSoldAmount.getText().equalsIgnoreCase("") && cbbCampaign.getSelectedIndex() != 0) {
            return true;
        }
        return false;
    }

    /**
     *
     */
    private void putDataForCbb() {
        arrCam = camBuz.findByActual();
        cbbCampaign.removeAllItems();
        cbbCampaign.addItem("--Select Campaign--");
        for (int i = 0; i < arrCam.size(); i++) {
            cbbCampaign.addItem(arrCam.get(i).getCampaignName());
        }
    }

    /**
     * 
     */
    private void reset() {
        txfActualBudget.setText("");
        txfActualSale.setText("");
        txfSaleVariance.setText("");
        txfSoldAmount.setText("");
        txfBudgetVariance.setText("");
        cbbCampaign.setSelectedIndex(0);
    }

    /**
     *
     * @return ActualBudget atb
     */
    private ActualBudget getObj() {
        ActualBudget atb = new ActualBudget();
        if (atb.validate(txfSoldAmount.getText(), txfActualSale.getText(), txfActualBudget.getText())) {
            arrCam = camBuz.findByActual();
            int indexRow = cbbCampaign.getSelectedIndex() - 1;
            int campaignID = arrCam.get(indexRow).getCampaignID();
            atb.setCampaignID(campaignID);
            atb.setSoldAmount(Integer.parseInt(txfSoldAmount.getText()));
            atb.setActualSale(Float.parseFloat(txfActualSale.getText()));
            atb.setActualBudget(Float.parseFloat(txfActualBudget.getText()));
            float actualSale = Float.parseFloat(txfActualSale.getText());
            float actualBudget = Float.parseFloat(txfActualBudget.getText());
            atb.setSaleVariance(camBuz.calSaleVariance(campaignID, actualSale));
            atb.setBudgetVariance(camBuz.calbudgetVariance(campaignID, actualBudget));
            return atb;
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        pnlIcon = new javax.swing.JPanel();
        lblcon = new javax.swing.JLabel();
        pnlActual = new javax.swing.JPanel();
        lblCampaign = new javax.swing.JLabel();
        cbbCampaign = new javax.swing.JComboBox();
        lblSoldAmount = new javax.swing.JLabel();
        lblActualSale = new javax.swing.JLabel();
        lblActualBudget = new javax.swing.JLabel();
        btnReset = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        txfSoldAmount = new javax.swing.JTextField();
        txfActualSale = new javax.swing.JTextField();
        txfActualBudget = new javax.swing.JTextField();
        txfSaleVariance = new javax.swing.JTextField();
        txfBudgetVariance = new javax.swing.JTextField();
        btnActualSale = new javax.swing.JButton();
        btnBudgetVariance = new javax.swing.JButton();
        pnlTitle = new javax.swing.JPanel();
        lblTitle = new javax.swing.JLabel();

        setMinimumSize(new java.awt.Dimension(100, 111));
        setPreferredSize(new java.awt.Dimension(100, 100));
        setLayout(new java.awt.GridBagLayout());

        pnlIcon.setMaximumSize(new java.awt.Dimension(1, 1));
        pnlIcon.setMinimumSize(new java.awt.Dimension(1, 1));
        pnlIcon.setPreferredSize(new java.awt.Dimension(200, 200));
        pnlIcon.setLayout(new java.awt.BorderLayout());

        lblcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/aptech/campaign/icon/ACTUAL.jpg"))); // NOI18N
        pnlIcon.add(lblcon, java.awt.BorderLayout.CENTER);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 139;
        gridBagConstraints.ipady = 402;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.6;
        gridBagConstraints.weighty = 0.8;
        add(pnlIcon, gridBagConstraints);

        pnlActual.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "INPUT ACTUAL BUDGET", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14), new java.awt.Color(51, 102, 255))); // NOI18N
        pnlActual.setMinimumSize(new java.awt.Dimension(1, 1));
        pnlActual.setLayout(new java.awt.GridBagLayout());

        lblCampaign.setText("Campaign : ");
        lblCampaign.setMinimumSize(new java.awt.Dimension(100, 20));
        lblCampaign.setPreferredSize(new java.awt.Dimension(100, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.ipady = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 6, 26, 0);
        pnlActual.add(lblCampaign, gridBagConstraints);

        cbbCampaign.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Select Campaign--", " " }));
        cbbCampaign.setMinimumSize(new java.awt.Dimension(220, 20));
        cbbCampaign.setPreferredSize(new java.awt.Dimension(200, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.ipadx = -66;
        gridBagConstraints.ipady = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 6, 26, 8);
        pnlActual.add(cbbCampaign, gridBagConstraints);

        lblSoldAmount.setText("Amount Product : ");
        lblSoldAmount.setMinimumSize(new java.awt.Dimension(100, 20));
        lblSoldAmount.setPreferredSize(new java.awt.Dimension(100, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 6, 26, 0);
        pnlActual.add(lblSoldAmount, gridBagConstraints);

        lblActualSale.setText("Actual Sale : ($)");
        lblActualSale.setMinimumSize(new java.awt.Dimension(100, 20));
        lblActualSale.setPreferredSize(new java.awt.Dimension(100, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 6, 26, 0);
        pnlActual.add(lblActualSale, gridBagConstraints);

        lblActualBudget.setText("Actual Budget : ($)");
        lblActualBudget.setMaximumSize(new java.awt.Dimension(200, 20));
        lblActualBudget.setMinimumSize(new java.awt.Dimension(100, 20));
        lblActualBudget.setPreferredSize(new java.awt.Dimension(100, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.ipadx = 20;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 6, 26, 0);
        pnlActual.add(lblActualBudget, gridBagConstraints);

        btnReset.setBackground(new java.awt.Color(51, 102, 255));
        btnReset.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btnReset.setForeground(new java.awt.Color(255, 255, 255));
        btnReset.setText("Reset");
        btnReset.setMinimumSize(new java.awt.Dimension(70, 25));
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 40, 0, 22);
        pnlActual.add(btnReset, gridBagConstraints);

        btnSave.setBackground(new java.awt.Color(51, 102, 255));
        btnSave.setFont(new java.awt.Font("Tahoma", 1, 14));
        btnSave.setForeground(new java.awt.Color(255, 255, 255));
        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 13, 0, 0);
        pnlActual.add(btnSave, gridBagConstraints);

        txfSoldAmount.setColumns(15);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.ipadx = 120;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 6, 26, 8);
        pnlActual.add(txfSoldAmount, gridBagConstraints);

        txfActualSale.setColumns(15);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.ipadx = 120;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 6, 26, 0);
        pnlActual.add(txfActualSale, gridBagConstraints);

        txfActualBudget.setColumns(15);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.ipadx = 120;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 6, 26, 8);
        pnlActual.add(txfActualBudget, gridBagConstraints);

        txfSaleVariance.setColumns(15);
        txfSaleVariance.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.ipadx = 120;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 6, 26, 8);
        pnlActual.add(txfSaleVariance, gridBagConstraints);

        txfBudgetVariance.setColumns(15);
        txfBudgetVariance.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.ipadx = 120;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 6, 26, 8);
        pnlActual.add(txfBudgetVariance, gridBagConstraints);

        btnActualSale.setText("SaleVariance (%)");
        btnActualSale.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnActualSaleActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.insets = new java.awt.Insets(0, 6, 26, 0);
        pnlActual.add(btnActualSale, gridBagConstraints);

        btnBudgetVariance.setText("BudgetVariance (%)");
        btnBudgetVariance.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBudgetVarianceActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.insets = new java.awt.Insets(0, 6, 26, 0);
        pnlActual.add(btnBudgetVariance, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 254;
        gridBagConstraints.ipady = 136;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.4;
        gridBagConstraints.weighty = 0.8;
        add(pnlActual, gridBagConstraints);

        pnlTitle.setBackground(new java.awt.Color(51, 102, 255));
        pnlTitle.setMinimumSize(new java.awt.Dimension(252, 40));
        pnlTitle.setLayout(new java.awt.BorderLayout());

        lblTitle.setBackground(new java.awt.Color(51, 102, 255));
        lblTitle.setFont(new java.awt.Font("Tahoma", 1, 30));
        lblTitle.setForeground(new java.awt.Color(51, 102, 255));
        lblTitle.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/aptech/campaign/icon/actualBanner.jpg"))); // NOI18N
        lblTitle.setMinimumSize(new java.awt.Dimension(252, 50));
        pnlTitle.add(lblTitle, java.awt.BorderLayout.CENTER);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.2;
        add(pnlTitle, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        reset();
    }//GEN-LAST:event_btnResetActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        if (!valid()) {
            JOptionPane.showMessageDialog(this, "Please fill Fields!");
        } else if (getObj() == null) {
            JOptionPane.showMessageDialog(this, "Please Enter number values!");
        } else if (camBuz.inputActualBudget(getObj())) {
            JOptionPane.showMessageDialog(this, camBuz.getLasterror());
            putDataForCbb();
            reset();
        }

    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnActualSaleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnActualSaleActionPerformed
        if (getObj() == null) {
            JOptionPane.showMessageDialog(this, "Please Enter number values!");
        } else if (cbbCampaign.getSelectedIndex() != 0 && !txfActualSale.getText().equalsIgnoreCase("")) {
            txfSaleVariance.setText(String.valueOf(getObj().getSaleVariance()) + " %");
        } else {
            JOptionPane.showMessageDialog(this, "Please fill Fields!");
        }
    }//GEN-LAST:event_btnActualSaleActionPerformed

    private void btnBudgetVarianceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBudgetVarianceActionPerformed
        if (getObj() == null) {
            JOptionPane.showMessageDialog(this, "Please Enter number values!");
        } else if (cbbCampaign.getSelectedIndex() != 0 && !txfActualBudget.getText().equalsIgnoreCase("")) {
            txfBudgetVariance.setText(String.valueOf(getObj().getBudgetVariance()) + " %");
        } else {
            JOptionPane.showMessageDialog(this, "Please fill Fields!");
        }
    }//GEN-LAST:event_btnBudgetVarianceActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnActualSale;
    private javax.swing.JButton btnBudgetVariance;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnSave;
    private javax.swing.JComboBox cbbCampaign;
    private javax.swing.JLabel lblActualBudget;
    private javax.swing.JLabel lblActualSale;
    private javax.swing.JLabel lblCampaign;
    private javax.swing.JLabel lblSoldAmount;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JLabel lblcon;
    private javax.swing.JPanel pnlActual;
    private javax.swing.JPanel pnlIcon;
    private javax.swing.JPanel pnlTitle;
    private javax.swing.JTextField txfActualBudget;
    private javax.swing.JTextField txfActualSale;
    private javax.swing.JTextField txfBudgetVariance;
    private javax.swing.JTextField txfSaleVariance;
    private javax.swing.JTextField txfSoldAmount;
    // End of variables declaration//GEN-END:variables
}
